package com.zt.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zt.reggie.common.CustomException;
import com.zt.reggie.entity.Category;
import com.zt.reggie.entity.Dish;
import com.zt.reggie.entity.SetMeal;
import com.zt.reggie.mapper.CategoryMapper;
import com.zt.reggie.service.CategoryService;
import com.zt.reggie.service.DishService;
import com.zt.reggie.service.SetMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author 挚爱之夕
 * @version 1.0
 * @implSpec com.zt.reggie.service.impl
 * @since 2023 - 02 - 25 - 14:41
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetMealService setMealService;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        int count = dishService.count(dishLambdaQueryWrapper);
        //查询当前分类是否关联了菜品
        if(count > 0){
            //关联了菜品，无法删除，抛出业务异常
            throw new CustomException("当前分类已关联菜品，不能删除!");
        }

        LambdaQueryWrapper<SetMeal> setMealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setMealLambdaQueryWrapper.eq(SetMeal::getCategoryId, id);
        count = setMealService.count(setMealLambdaQueryWrapper);
        if(count > 0){
            //关联了套餐，无法删除，抛出业务异常
            throw new CustomException("当前分类已关联套餐，不能删除!");
        }
        //删除
        super.removeById(id);
    }
}
